//
//  BJStudyTestViewController.m
//  DangXiao
//
//  Created by APPLE on 16/12/7.
//  Copyright © 2016年 APPLE. All rights reserved.
//

#import "BJStudyTestViewController.h"
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "TestViewController.h"

@interface BJStudyTestViewController ()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>
/**
 *  图片数组
 */
@property (nonatomic, strong) NSMutableArray *imageArray;

/**
 *  指示label
 */
@property (nonatomic, strong) UILabel *indicateLabel;
@end

@implementation BJStudyTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"学习测试";
//    for (int index = 0; index < 5; index++) {
////        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",index]];
//        [self.imageArray addObject:index];
//    }
     [self setupUI];
}

#pragma mark --push控制器
- (void)pushVC {
    
    TestViewController *testVC = [[TestViewController alloc] init];
    
    [self.navigationController pushViewController:testVC animated:YES];
}

- (void)setupUI {
    
    
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, BILIHEIGHT(10), ScreenWidth, ScreenHeight - BILIHEIGHT(80))];
    CGFloat a = 999999999999999999;
    pageFlowView.autoTime = a;
    pageFlowView.backgroundColor = [UIColor whiteColor];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.1;
    pageFlowView.minimumPageScale = 0.85;
    pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    
    //提前告诉有多少页
    //    pageFlowView.orginPageCount = self.imageArray.count;
    
    pageFlowView.isOpenAutoScroll = YES;
    
    //初始化pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, pageFlowView.frame.size.height - 24 - 8, ScreenWidth, 8)];
    pageFlowView.pageControl = pageControl;
//    [pageFlowView addSubview:pageControl];
    
    /****************************
     使用导航控制器(UINavigationController)
     如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
     请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
     *****************************/
    
    UIScrollView *bottomScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [bottomScrollView addSubview:pageFlowView];
    //    bottomScrollView.backgroundColor = [UIColor greenColor];
    [pageFlowView reloadData];
    [self.view addSubview:bottomScrollView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
//    return CGSizeMake(ScreenWidth - 84, (ScreenWidth - 84) * 9 / 16);
    return CGSizeMake(ScreenWidth - 84, ScreenHeight - BILIHEIGHT(100));
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    DLog(@"点击了第%ld张图",(long)subIndex + 1);
    self.indicateLabel.text = [NSString stringWithFormat:@"点击了第%ld张图",(long)subIndex + 1];
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return self.imageArray.count;
}

- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = (PGIndexBannerSubiew *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 84, ScreenHeight - BILIHEIGHT(100))];
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
        bannerView.backgroundColor = [UIColor purpleColor];
    }
    //在这里下载网络图片
    //  [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:hostUrlsImg,imageDict[@"img"]]] placeholderImage:[UIImage imageNamed:@""]];
//    bannerView.mainImageView.image = self.imageArray[index];
    
    
    
    if (index == 2) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
        label.backgroundColor = BaseColor;
        [bannerView addSubview:label];
    }
    
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    
    DLog(@"ViewController 滚动到了第%ld页",pageNumber);
    if (pageNumber == 0) {
        DLog(@"我是第一页");
    }
}

#pragma mark --懒加载
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"",@"", nil];
    }
    return _imageArray;
}

- (UILabel *)indicateLabel {
    
    if (_indicateLabel == nil) {
        _indicateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, ScreenWidth, 16)];
        _indicateLabel.textColor = [UIColor blueColor];
        _indicateLabel.font = [UIFont systemFontOfSize:16.0];
        _indicateLabel.textAlignment = NSTextAlignmentCenter;
        _indicateLabel.text = @"指示Label";
    }
    return _indicateLabel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
